#include "c3_main.h"
#include "socket.h"
#include "gatts_table_creat_demo.h" //ble蓝牙

#include <sys/socket.h>
#include <arpa/inet.h>

static const char *TAG = "socket: ";

#define PORT 8080
#define IP_ADDR "192.168.1.12"

static int sockfd;
static struct sockaddr_in server_addr;
static char buffer[HUAN_CHONG_QU_LEN];

size_t xBytesSent;
const TickType_t x100ms = pdMS_TO_TICKS(100);

bool init_sockets(uint16_t port, char *ip)
{
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        //ESP_LOGI(TAG, "Socket creation failed 005 \n\r");
        return false;
    }

    // 设置server地址
    // uint32_t ip = htonl((192 << 24) | (168 << 16) | (1 << 8) | 12);//硬编程ip地址
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = inet_addr(ip); // 将字符串转换为网络字节序的整数值

    // 连接到server
    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        //ESP_LOGI(TAG, "Connection failed 004 \n\r");
        return false;
    }
    return true;
}

bool sockets_faSong(char *message)
{
    if (send(sockfd, message, strlen(message), 0) < 0)
    {
        //ESP_LOGI(TAG, "fa song shi bai 003 \n\r");
        return false;
    }
    return true;
}

int16_t sockets_jieShou()
{
    memset(buffer, 0, sizeof(buffer));
    while (1)
    {
        int recvd = recv(sockfd, buffer, sizeof(buffer), 0);
        if (recvd < 0)
        {
            if (errno == EAGAIN)
            {             // 如果recv返回-1并且errno为EAGAIN，表示可以再次尝试接收
                continue; // 结束循环,从新开始
                return 1;
            }
            else
            {
                // printf("Receive failed: %s\n", strerror(errno)); // 打印出具体的错误信息
                //ESP_LOGI(TAG, "Receive failed 002 = %s\n", strerror(errno));
                close_sockets(); // 注销连接
                return 2;
                break;
            }
        }
        else if (recvd == 0)
        { // 如果recv返回0，表示对端已关闭连接
            return 3;
            break;
        }
        else if (recvd > ZHUAN_FA_XIAO_XI)
        {
            return 4;//缓冲区溢出
        }
        else
        {
            // 如果recv返回正数，表示接收到数据
            //buffer[recvd] = '\0';               // 在接收到的字符串末尾添加'\0'
            ESP_LOGW(TAG,"接收数据 =  %s\n", buffer);// 打印出server的响应
            /*这里将buffer数组当字符串(char*类型)传递,但是'\0'后面的空数据不需要传递,节约资源,
             *recvd就是收到的数据长度,因为数组下标从0开始,recvd长度的数据实际上包含了'\0',传递的是一个完整的字符串*/
            // xBytesSent = xMessageBufferSend(xMessageBuffer, (void *)buffer, sizeof(buffer), x100ms);
            xBytesSent = xMessageBufferSend(zhuanFanP, (void *)buffer, strlen((char *)buffer), x100ms);
            if (xBytesSent != strlen((char *)buffer))
            {
                ESP_LOGI(TAG, "sockets  huan chong qu fa song shi bai 002\n");
            }
            return 0;
            break; // 接收到数据后跳出循环
        }
    }
}

void close_sockets()
{
    // 关闭socket
    //ESP_LOGW(TAG, "sockets guan bi lian jie 001\n");
    close(sockfd);
}
